const FoodObj = require('../modules/Food');
const fs = require('fs');
const path = require('path');

let FoodController = {
    get_foodInfo: async function(req, res){
        let resultDate = await FoodObj.getFoodInfo();
        res.send(resultDate);
    },

    get_img: async function (req, res) {
        let data = req.query
        console.log(data);
        const filePath = path.resolve(__dirname, `../../../public/images/${data.url}`);
        // 给客户端返回一个文件流 type类型
        res.set('content-type', { "png": "image/png", "jpg": "image/jpeg" });//设置返回类型
        var stream = fs.createReadStream(filePath);
        var responseData = [];//存储文件流
        if (stream) {//判断状态
            stream.on('data', function (chunk) {
                responseData.push(chunk);
            });
            stream.on('end', function () {
                var finalData = Buffer.concat(responseData);
                res.write(finalData);
                res.end();
            });
        }
    }
}
module.exports = FoodController;